<template>
  <div class="toolbar colorMain">
    <div class="mt-10 mb-10">
      <el-form :inline="true" size="small">
        <el-form-item label="时间"><date-picker v-model="queryFrom.time_range"/></el-form-item>
        <el-form-item label="时段类型" ><time-interval v-model="queryFrom.interval_type"/></el-form-item>
        <el-form-item label="游戏类型" size="small">
          <div v-if="['witch', 'loki'].includes($store.state.user.business)">
            <el-select v-model="queryFrom.game_type" placeholder="请选择" @visible-change="viChange">
              <el-option v-for="(value, key) of chooseOptions" :key="key" :label="value.label" :value="value.value"/>
            </el-select>
          </div>
          <select-all v-else :all-select-list="chooseOptions" v-model="queryFrom.game_type" />
        </el-form-item>
        <el-form-item label="玩法" size="small">
          <select-all :all-select-list="options" v-model="queryFrom.game_id_list" />
        </el-form-item>
        <el-form-item label="用户类型"><user-type v-model="queryFrom.user_type"/></el-form-item>
        <el-form-item label="设备类型" >
          <el-select v-model="queryFrom.chn_device_type" clearable placeholder="请选择">
            <el-option
                    v-for="(value,key) of chn_device_typeop"
                    :key="key"
                    :label="value"
                    :value="key"
                    sortable/>
          </el-select>
        </el-form-item>
        <el-button size="small" type="primary" @click="sendQuery">提交</el-button>
      </el-form>
      <div><el-button :loading="excelLoad" icon="el-icon-printer" style="float: right;margin: 0 0 10px 0" size="mini" type="success" @click="downExcel" >导出数据</el-button></div>
      <analyse-table v-if="showTable" :table-data="tableData" :key_mapper="key_mapper" :sortable = "[]" :min-width = "120" size="small" @sort-change="sortChange"/>
      <el-pagination :current-page.sync="page" :page-size="size" :total.sync="total" class="mt-10" layout="total, sizes, prev, pager, next, jumper" @current-change="currentChange" @size-change="handleSizeChange"/>
    </div>
  </div>
</template>

<script>
import { getGameAnalysis, getDwcGameType } from '@/api/table'
import { pickTime } from '@/model/DateRange'
// import { lotteryoptions, ttoptions, zhanshengoptions, heijinoptions, zyoptions } from '@/model/DWC'
import mixins from '@/model/mixins'

// const Obj = {
//   zs: zhanshengoptions,
//   tt: ttoptions,
//   loki: lotteryoptions,
//   dark3: heijinoptions,
//   dark2: zyoptions
// }

const ObjLable = {
  loki: '彩票',
  DWC: '玩法'
}

const lotteryType = ['witch', 'loki']

export default {
  name: 'DWC',
  mixins: [mixins],
  data() {
    return {
      label: ObjLable[this.$store.state.user.business] || '玩法',
      options: [],
      // chooseOptions: Obj[this.$store.state.user.business] || lotteryoptions,
      chooseOptions: [],
      queryFrom: {
        game_type: lotteryType.includes(this.$store.state.user.business) ? '' : [],
        user_type: '',
        game_id_list: [],
        time_range: pickTime
      },
      API: getGameAnalysis,
      showTable: true,
      getSomeData: '',
      goodObj: {},
      chn_device_typeop: {
        ios: 'IOS',
        android: 'Android',
      }
    }
  },
  watch: {
    'queryFrom.game_type': function() {
      if (lotteryType.includes(this.$store.state.user.business)) {
        if (this.queryFrom.game_type) {
          this.options = this.chooseOptions.filter(item => {
            return (item.value === this.queryFrom.game_type)
          })[0].child
        }
      } else {
        if (this.queryFrom.game_type.length === 1) {
          this.options = this.chooseOptions.filter(item => {
            return (item.value === this.queryFrom.game_type[0])
          })[0].child
        } else {
          this.options = []
          this.queryFrom.game_id_list = []
        }
      }
      // console.log(this.options)
      console.log(ObjLable[this.$store.state.user.business])
    },
    tableData() {
      this.showTable = false
      this.$nextTick(() => (
        this.showTable = true
      ))
    }
  },
  created() {
    this.getDwcGameType()
  },
  methods: {
    change() {
      this.queryFrom.game_id_list = []
    },
    viChange() {
      this.queryFrom.game_id_list = []
    },
    getDwcGameType() {
      getDwcGameType().then(res => {
        if (res.status === 0) {
          // 数组第一层更改键
          this.goodObj = res.data.preset.map(function(item) {
            return {
              value: item.key,
              label: item.name,
              child: item.third_type
            }
          })
          // 数组第一层加键值对 二层改键/加键值对
          this.goodObj.map(function(item) {
            item['name'] = '全选/取消'
            item['CName'] = item.label
            item.child.map(function(item1) {
              item1['value'] = item1.key
              delete item1.key
              item1['CName'] = item1.name
              delete item1.name
              item1['name'] = '全选/取消'
            })
          })
          this.chooseOptions = this.goodObj
        }
      })
    }
  }
}
</script>
